/*
 * @Author       : tfhappy
 * @Date         : 2020-07-09 00:27:46
 * @LastEditTime : 2020-07-10 00:31:00
 * @FilePath     : /giteecli/action/repository.go
 * @Description  :
 */
package action

import (
	"fmt"

	"github.com/urfave/cli/v2"

	"gitee.com/tfhappy/giteecli/api"
	"gitee.com/tfhappy/giteecli/common"
)

func CreateRepositoryAction(c *cli.Context) error {
	accessToken := GetAuthToken(c)
	repoName := c.String("name")
	homepage := c.String("homepage")
	username := c.String(common.USERNAME)
	if homepage == "" {
		homepage = fmt.Sprintf("https://gitee.com/%s/%s", username, repoName)
	}

	createRepoReq := &api.CreateRepositoryRequest{
		Name:              repoName,
		AccessToken:       accessToken,
		Description:       c.String("description"),
		Homepage:          homepage,
		HasIssues:         c.Bool("has_issues"),
		HasWiki:           c.Bool("has_wiki"),
		CanComment:        c.Bool("can_comment"),
		AutoInit:          c.Bool("auto_init"),
		GitignoreTemplate: c.String("gitignore_template"),
		LicenseTemplate:   c.String("license_template"),
		Private:           c.Bool("private"),
	}

	resp, err := api.CreateRepository(createRepoReq)
	if err != nil {
		panic(err)
	}
	b, err := common.JSON.MarshalIndent(resp, "", "    ")
	fmt.Println(string(b))
	fmt.Println("addr:")
	fmt.Printf("git clone git@gitee.com:%s/%s.git", username, repoName)
	return err
}
